package routers

import (
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/context"
	"github.com/astaxie/beego/plugins/cors"
	"hello/controllers/Backend"
	"hello/routers/Middleware"
	"hello/wl"
	"time"
)

func init() {

	beego.Include(	&Backend.PostController{},
					&Backend.TestController{},
					&Backend.XinController{},
					&Backend.UserController{},
					&Backend.UploadController{},
					&Backend.OAuthController{},
					)

	var FilterFunc = func(ctx *context.Context)() {
		fmt.Println("======="+time.Now().Format("2006-01-02 15:04:05")+"=======")
	}

	var FilterFunc1 = func(ctx *context.Context)() {
		fmt.Println("在此之前")
	}
	//文章这块
	postRouter :=
		beego.NewNamespace("/post",

			beego.NSBefore(FilterFunc1),

			beego.NSRouter("/", &Backend.PostController{},"get:Posts"),
			//创建
			beego.NSRouter("/create", &Backend.PostController{}, "post:PostAdd"),
			//删除
			beego.NSRouter("/delete/:id ", &Backend.PostController{}, "get:PostDel"),
			//编辑
			beego.NSRouter("/edit ", &Backend.PostController{}, "post:PostEditAction"),
			//查询
			beego.NSRouter("/show/:id ", &Backend.PostController{}, "get:PostShow"),
			//赞
			beego.NSNamespace("/zan",
				//点赞和取消
				beego.NSRouter("/action/:id ", &Backend.PostController{}, "get:ZanAction"),
				//是否赞
				beego.NSRouter("/isZan/:post_id ", &Backend.PostController{}, "get:IsZanAction"),
			),
			//评论
			beego.NSNamespace("/comment",
				//添加评论
				beego.NSRouter("/create ", &Backend.PostController{}, "post:CommentCreateAction"),
			),

	)
	//用户这块
	userRouter:=
		beego.NewNamespace("/user",
			beego.NSBefore(FilterFunc1),
			//登陆
			beego.NSRouter("/login", &Backend.UserController{},"post:UserLogin"),
			//注册
			beego.NSRouter("/register", &Backend.UserController{},"post:UserRegister"),
			//用户信息(公共)
			beego.NSRouter("/info/:id", &Backend.UserController{},"get:UserInfo"),
			//用的文章,点赞文章,评论文章
			beego.NSRouter("/post/:action/:id", &Backend.UserController{},"get:UserPost"),
			//检查是否登陆
			beego.NSRouter("/checklogin", &Backend.UserController{},"get:CheckLogin"),
			//修改用户信息
			beego.NSRouter("/update", &Backend.UserController{},"post:UpdateUser"),
			//关注取消关注
			beego.NSRouter("/fan/:id", &Backend.UserController{},"get:FanAction"),
			//粉丝和关注数量
			beego.NSRouter("/fanCount/:id", &Backend.UserController{},"get:FansCount"),
			//粉丝和关注 的用户列表
			beego.NSRouter("/fanUsers/:id/:action", &Backend.UserController{},"get:FanUsers"),
			//是否关注
			beego.NSRouter("/isfan/:id", &Backend.UserController{},"get:IsFan"),

			beego.NSRouter("/test/:id", &Backend.UserController{},"get:Index"),
			beego.NSRouter("/test2", &Backend.UserController{},"get:Index2"),
			beego.NSRouter("/test3", &Backend.UserController{},"get:Index3"),
	)

	//测试
	TestRouter:=
		beego.NewNamespace("/test",

			beego.NSRouter("/test", &Backend.TestController{},"get:Index"),
			beego.NSRouter("/test2", &Backend.UserController{},"get:Index2"),
			beego.NSRouter("/test3", &Backend.UserController{},"get:Index3"),
			beego.NSRouter("/test4", &Backend.OAuthController{},"get:QQ"),
			beego.NSRouter("/test5", &Backend.OAuthController{},"get:QQLogin"),
			beego.NSRouter("/Upload", &Backend.TestController{},"post:Upload"),
		)

	//权限
	OAuthRouter:=
		beego.NewNamespace("/oauth",

			beego.NSRouter("/github/url", &Backend.OAuthController{},"get:LoginUrl"),
			beego.NSRouter("/github/action", &Backend.OAuthController{},"get:AuthLogin"),

		)
	//上传
	UploadRouter:=
		beego.NewNamespace("/upload",

			beego.NSRouter("/jpg", &Backend.UploadController{},"post:Index"),
			beego.NSRouter("/ckedit", &Backend.UploadController{},"post:CKIndex"),

		)
	//其他
	UtilsRouter:=
		beego.NewNamespace("/utils",

			beego.NSRouter("/count", &Backend.UtilsController{},"get:AjaxCount"),

		)

	beego.AddNamespace(postRouter,userRouter,TestRouter,UploadRouter,OAuthRouter,UtilsRouter)


	//跨域
	beego.InsertFilter("*", beego.BeforeRouter, cors.Allow(&cors.Options{
		AllowAllOrigins:  true,
		AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
		AllowHeaders:     []string{"Origin", "Authorization", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
	}))
	//中间键
	beego.InsertFilter("*", beego.BeforeRouter, FilterFunc)
	beego.InsertFilter("*", beego.BeforeRouter, func(ctx *context.Context) {
		if wl.C("token")=="true"{
			Middleware.ValidateToken(ctx)
		}

		//验证请求
		Middleware.Get(ctx)
	})




}
